<?php

class CollectionsController extends Controller
{
	public function actionGet() { 
		$id = Yii::app()->request->getParam('pid',null);
		$collections = Collections::model()->findAllByAttributes(array(
			'object_id'=>outBin($id)
		));
		if(!empty($collections)){
			$data = array();
			foreach( $collections as $key=>$collection){
				$collection->id = outStr($collection->id);
				$collection->object_id = outStr($collection->object_id);
				$data[] = $collection->attributes;
			}
			
			jsonOut(array(
				'error'=>false,
				'data'=>$data
			));
		}else{
			jsonOut(array(
				'error'=>false,
				'data'=>array()
			));
		}
	}
	
	public function actionSave() { 
		if(Yii::app()->user->isGuest){
			jsonOut(array(
				'error'=>true,
				'message'=>'Pls login'
			));
		}
		if(empty($_POST)){
			jsonOut(array(
				'error'=>true,
				'message'=>'Form params not found'
			));
		}
		$id = Yii::app()->request->getParam('pid',null);
		$project = Projects::model()->findByPk(outBin($id));
		if(empty($project)){
			jsonOut(array('error'=>true,'message'=>'Project not found'));
		}

		$c = Collections::model()->findAllByAttributes(array(
			'name'=>$_POST['name'],
			'object_id'=>$project->id
		));
		if(!empty($c)) jsonOut(array('error'=>true,'message'=>'Collection are already exists'));
		$model = new Collections;
		$model->object_id = $project->id;
		$model->created = time();
		$model->name = $_POST['name'];
		
		
		try{
			$model->save();
			$model->id = outStr($model->id);
			jsonOut(array( 'error'=>false,'message'=>'The collections has been successfully' ,'data'=>$model->attributes));
		}catch(Exception $e){
			jsonOut(array('error'=>true,'message'=>$e->getMessage()));
		}
		
	} 
	/**
	 * COLLECTION ATTRIBUTES
	 **/
	public function actionGetAttr() { 
		$id = Yii::app()->request->getParam('pid',null);
		$collections = CmsAttr::model()->findAllByAttributes(array(
			'collection_id'=>outBin($id)
		));
		if(!empty($collections)){
			$data = array();
			foreach( $collections as $key=>$collection){
				$collection->id = outStr($collection->id);
				$collection->collection_id = outStr($collection->collection_id);
				$collection->created = date("Y-m-d H:i:s",$collection->created);
				$data[] = $collection->attributes;
			}
			
			jsonOut(array(
				'error'=>false,
				'data'=>$data
			));
		}else{
			jsonOut(array(
				'error'=>false,
				'data'=>array()
			));
		}
	}
	public function actionRemoveAttr() { 
		if(Yii::app()->user->isGuest){
			jsonOut(array(
				'error'=>true,
				'message'=>'Pls login'
			));
		}
		$id = Yii::app()->request->getParam('pid',null);
		$c = CmsAttr::model()->findByPk(outBin($id));
		try{
			if(!empty($c)) $c->delete();
			
			jsonOut(array('error'=>false,'message'=>''));
		}catch(Exception $e){
			jsonOut(array('error'=>true,'message'=>$e->getMessage()));
		}
	}
	public function actionSaveAttr() { 
		if(Yii::app()->user->isGuest){
			jsonOut(array(
				'error'=>true,
				'message'=>'Pls login'
			));
		}
		if(empty($_POST)){
			jsonOut(array(
				'error'=>true,
				'message'=>'Form params not found'
			));
		}
		$id = Yii::app()->request->getParam('pid',null);
		$Collections = Collections::model()->findByPk(outBin($id));
		if(empty($Collections)){
			jsonOut(array('error'=>true,'message'=>'Collections not found'));
		}

		$c = CmsAttr::model()->findAllByAttributes(array(
			'attr'=>$_POST['name'],
			'collection_id'=>$Collections->id
		));
		if(!empty($c)) jsonOut(array('error'=>true,'message'=>'Collection attributes are already exists'));
		$model = new CmsAttr;
		$model->collection_id = $Collections->id;
		$model->created = time();
		$model->attr = $_POST['name'];
		
		
		try{
			$model->save();
			$model->id = outStr($model->id);
			$model->created = date("Y-m-d H:i:s",$model->created);
			jsonOut(array( 'error'=>false,'message'=>'The collections has been successfully' ,'data'=>$model->attributes));
		}catch(Exception $e){
			jsonOut(array('error'=>true,'message'=>$e->getMessage()));
		}
		
	}
	
	/**
	 * COLLECTION VALUES
	 **/
	public function actionSaveValue() { 
		if(Yii::app()->user->isGuest){
			jsonOut(array(
				'error'=>true,
				'message'=>'Pls login'
			));
		}
		if(empty($_POST)){
			jsonOut(array(
				'error'=>true,
				'message'=>'Form params not found'
			));
		}
		$id = Yii::app()->request->getParam('pid',null);
		$Collections = Collections::model()->findByPk(outBin($id));
		if(empty($Collections)){
			jsonOut(array('error'=>true,'message'=>'Collections not found'));
		}
		
		$model = new CmsValue;
		$model->collection_id = $Collections->id;
		$model->created = time();
		$model->content = @CJSON::encode($_POST);
		
		
		try{
			$model->save();
			$model->id = outStr($model->id);
			$model->created = date("Y-m-d H:i:s",$model->created);
			$d = @CJSON::decode($model->content);
				
			$str = "";
			foreach($d as $k=>$v){
				$str.= $k.": ". $v." <br>";
			}
			$model->content = $str;
			jsonOut(array( 'error'=>false,'message'=>'The collections has been successfully' ,'data'=>$model->attributes));
		}catch(Exception $e){
			jsonOut(array('error'=>true,'message'=>$e->getMessage()));
		}
		
	}
	public function actionGetAttrValue() { 
		$id = Yii::app()->request->getParam('pid',null);
		$collections = CmsValue::model()->findAllByAttributes(array(
			'collection_id'=>outBin($id)
		));
		if(!empty($collections)){
			$data = array();
			foreach( $collections as $key=>$collection){
				$collection->id = outStr($collection->id);
				$collection->collection_id = outStr($collection->collection_id);
				$collection->created = date("Y-m-d H:i:s",$collection->created);
				$d = @CJSON::decode($collection->content);
				
				$str = "";
				foreach($d as $k=>$v){
					$str.= $k.": ". $v." <br>";
				}
				$collection->content = $str;
				$data[] = $collection->attributes;
			}
			
			jsonOut(array(
				'error'=>false,
				'data'=>$data
			));
		}else{
			jsonOut(array(
				'error'=>false,
				'data'=>array()
			));
		}
	}
	public function actionRemoveAttrValue() { 
		if(Yii::app()->user->isGuest){
			jsonOut(array(
				'error'=>true,
				'message'=>'Pls login'
			));
		}
		$id = Yii::app()->request->getParam('pid',null);
		$c = CmsValue::model()->findByPk(outBin($id));
		try{
			if(!empty($c)) $c->delete();
			
			jsonOut(array('error'=>false,'message'=>''));
		}catch(Exception $e){
			jsonOut(array('error'=>true,'message'=>$e->getMessage()));
		}
	}
	
}